home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / moreappleevents / aehelpers.c next >
Encoding:
Text File  |  2000-06-23  |  9.8 KB  |  390 lines

  1. /*
  2.     File:        AEHelpers.c
  3.  
  4.     Contains:    Functions to help you when you are building and sending Apple events.
  5.  
  6.     Written by: Andy Bachorski    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/21/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. //    Conditionals to setup the build environment the way we like it.
  25. #include "PrivateConditionals.h"
  26.  
  27.  
  28. //**********    Universal Headers        ****************************************
  29.  
  30. #include <AERegistry.h>
  31. #include <AEObjects.h>
  32. #include <AEPackObject.h>
  33. #include <Gestalt.h>
  34. #include <Traps.h>
  35.  
  36. #if UNIVERSAL_INTERFACES_VERSION >= 0x0300
  37.     #include <FinderRegistry.h>
  38. #else
  39.     #include "FinderRegistry.h"
  40. #endif
  41.  
  42. //**********    Project Headers            ****************************************
  43.  
  44. #include "AEHelpers.h"
  45. #include "ProcessHelpers.h"
  46.  
  47.  
  48. //**********    Private Definitions        ****************************************
  49.  
  50. #define    kFlagNotSet    -1
  51.  
  52.  
  53. //******************************************************************************
  54.  
  55.  
  56. OSErr AEHMakeEventSelfTarget( const AEEventClass eventClass,
  57.                               const AEEventID eventID,
  58.                               AppleEvent *theEventPtr )
  59. {
  60.     OSErr    anErr = noErr;
  61.     
  62.     ProcessSerialNumber        selfPSN = { 0, kCurrentProcess };
  63.     
  64.     anErr = AEHMakeEventProcessTarget( &selfPSN, eventClass, eventID, theEventPtr );
  65.     
  66.     return ( anErr );
  67. }//end AEHMakeEventSelfTarget
  68.  
  69. //******************************************************************************
  70.  
  71. pascal    OSErr    AEHMakeEventSignatureTarget( const OSType targetType,
  72.                                                   const OSType targetCreator,
  73.                                                   const AEEventClass eventClass,
  74.                                                   const AEEventID eventID,
  75.                                                         AppleEvent *theEventPtr )
  76. {
  77.     OSErr    anErr = noErr;
  78.     
  79.     ProcessSerialNumber        psn = { kNoProcess, kNoProcess };
  80.     
  81.     anErr = FindProcessBySignature( targetType, targetCreator, &psn );
  82.     if ( anErr == noErr )
  83.     {
  84.         anErr = AEHMakeEventProcessTarget( &psn, eventClass, eventID, theEventPtr );
  85.     }
  86.     return anErr;
  87. }//end AEHMakeEventSignatureTarget
  88.  
  89. //******************************************************************************
  90.  
  91. pascal    OSErr    AEHMakeEventProcessTarget( const ProcessSerialNumberPtr psnPtr,
  92.                                            const AEEventClass eventClass,
  93.                                            const AEEventID eventID,
  94.                                                  AppleEvent *theEventPtr )
  95. {
  96.     OSErr    anErr = noErr;
  97.     AEDesc    targetAppDesc = { typeNull, nil };
  98.     
  99.     anErr = AECreateDesc (typeProcessSerialNumber, psnPtr, sizeof( ProcessSerialNumber ), &targetAppDesc);
  100.  
  101.     if ( anErr == noErr )
  102.     {
  103.         anErr = AECreateAppleEvent( eventClass, eventID, &targetAppDesc,
  104.                                     kAutoGenerateReturnID, kAnyTransactionID, theEventPtr);
  105.     }
  106.     
  107.     AEDisposeDesc( &targetAppDesc );
  108.     
  109.     return anErr;
  110. }//end AEHMakeEventProcessTarget
  111.  
  112. //******************************************************************************
  113.  
  114. pascal    OSErr    AEHMakeEventTargetID( const TargetID *targetIDPtr,
  115.                                       const AEEventClass eventClass,
  116.                                       const AEEventID eventID,
  117.                                             AppleEvent *theEventPtr )
  118. {
  119.     OSErr    anErr = noErr;
  120.     AEDesc    targetAppDesc = { typeNull, nil };
  121.     
  122.     anErr = AECreateDesc (typeTargetID, targetIDPtr, sizeof( TargetID ), &targetAppDesc);
  123.  
  124.     if ( anErr == noErr )
  125.     {
  126.         anErr = AECreateAppleEvent( eventClass, eventID, &targetAppDesc,
  127.                                     kAutoGenerateReturnID, kAnyTransactionID, theEventPtr);
  128.     }
  129.     
  130.     AEDisposeDesc( &targetAppDesc );
  131.     
  132.     return anErr;
  133. }//end AEHMakeEventProcessTarget
  134.  
  135. //******************************************************************************
  136.  
  137. pascal    OSErr    AEHSendEventReturnSInt16( const AEIdleUPP idleProcUPP,
  138.                                           const AppleEvent *theEvent,
  139.                                                 SInt16 *theValue )
  140. {
  141.     OSErr    anErr = noErr;
  142.     
  143.     if ( idleProcUPP == nil )
  144.     {
  145.         anErr = paramErr;    //    No idle function is an error, since we are expected to return a value
  146.     }
  147.     else
  148.     {
  149.         AppleEvent    theReply = {typeNull, nil};
  150.         AESendMode    sendMode = kAEWaitReply;
  151.         
  152.         anErr = AESend( theEvent, &theReply, sendMode, kAENormalPriority,
  153.                         kNoTimeOut, idleProcUPP, nil );
  154.         //    Don't dispose of the event, it's not ours
  155.         if ( anErr == noErr )
  156.         {
  157.             anErr =  AEHGetHandlerError( &theReply );
  158.             
  159.             if ( !anErr && theReply.descriptorType != typeNull )
  160.             {
  161.                 DescType    actualType;        //    we don't care about these, because we are not asking
  162.                 Size        actualSize;        //    typeWildcard, so we get an error if we dont' get what we want.
  163.                 
  164.                 anErr = AEGetParamPtr( theEvent, keyDirectObject, typeShortInteger,
  165.                                         &actualType, theValue, sizeof( SInt16 ), &actualSize );
  166.             }
  167.             (void) AEDisposeDesc( &theReply );
  168.         }
  169.     }
  170.     return anErr;
  171. }//end AEHSendEventReturnSInt16
  172.  
  173. //******************************************************************************
  174.  
  175. pascal    OSErr    AEHSendEventNoReturnValue( const AEIdleUPP idleProcUPP,
  176.                                            const AppleEvent *theEvent )
  177. {
  178.     OSErr        anErr = noErr;
  179.     AppleEvent    theReply = { typeNull, nil };
  180.     AESendMode    sendMode;
  181.     
  182.     if ( idleProcUPP == nil )
  183.         sendMode = kAENoReply;
  184.     else
  185.         sendMode = kAEWaitReply;
  186.  
  187.     anErr = AESend( theEvent, &theReply, sendMode, kAENormalPriority, kNoTimeOut, idleProcUPP, nil );
  188.     if ( anErr == noErr  &&  sendMode == kAEWaitReply )
  189.     {
  190.         anErr =  AEHGetHandlerError( &theReply );
  191.     }
  192.     (void) AEDisposeDesc( &theReply );
  193.     
  194.     return anErr;
  195. }//end AEHSendEventNoReturnValue
  196.  
  197. //******************************************************************************
  198.  
  199. pascal    OSErr    AEHGetHandlerError( const AppleEvent *reply )
  200. {
  201.     OSErr        anErr = noErr;
  202.     OSErr        handlerErr;
  203.     
  204.     DescType    actualType;
  205.     long        actualSize;
  206.     
  207.     if ( reply->descriptorType != typeNull )    // there's a reply, so there may be an error
  208.     {
  209.         OSErr    getErrErr = noErr;
  210.         
  211.         getErrErr = AEGetParamPtr( reply, keyErrorNumber, typeShortInteger, &actualType,
  212.                                     &handlerErr, sizeof( OSErr ), &actualSize );
  213.         
  214.         if ( getErrErr != errAEDescNotFound )    // found an errorNumber parameter
  215.         {
  216.             anErr = handlerErr;                    // so return it's value
  217.         }
  218.     }
  219.     return anErr;
  220. }//end AEHGetHandlerError
  221.  
  222. //**************************************************************************
  223.  
  224. OSErr AEHExtractClassAndID ( const AppleEvent *theEventPtr, AEEventClass *eventClass, AEEventID *eventID )
  225. {
  226.     DescType    actualType;
  227.     Size        actualSize;
  228.     OSErr        anErr;
  229.  
  230.     anErr = AEGetAttributePtr( theEventPtr, keyEventClassAttr, typeType, &actualType,
  231.                                 eventClass, sizeof( eventClass ), &actualSize );
  232.     if ( anErr == noErr )
  233.     {
  234.         anErr = AEGetAttributePtr( theEventPtr, keyEventIDAttr, typeType, &actualType,
  235.                                     eventID, sizeof( eventID ), &actualSize );
  236.     }
  237.     return ( anErr );
  238. }//end ExtractClassAndID
  239.  
  240. //**************************************************************************
  241.  
  242. pascal    Boolean    AEHSimpleIdleFunction( EventRecord *event,
  243.                                        long *sleepTime,
  244.                                        RgnHandle *mouseRgn )
  245. {
  246. #pragma unused( event )
  247.     *sleepTime = 30;
  248.     *mouseRgn = nil;
  249.     
  250.     return ( false );
  251. }//end AEHSimpleIdleFunction
  252.  
  253. //******************************************************************************
  254.  
  255. pascal Boolean GestaltAvailable( void )
  256. {
  257.     OSErr    anErr = noErr;
  258.     
  259.     static    long        gGestaltAvailable = kFlagNotSet;
  260.     
  261.     if ( gGestaltAvailable == kFlagNotSet )
  262.     {
  263.         if ( NGetTrapAddress(_Gestalt,kOSTrapType ) )
  264.         {
  265.             gGestaltAvailable = true;
  266.         }
  267.         else
  268.         {
  269.             gGestaltAvailable = false;
  270.         }
  271.     }
  272.     
  273.     return gGestaltAvailable;
  274. }//end GestaltAvailable
  275.  
  276. //******************************************************************************
  277.  
  278. pascal Boolean HasAppleEvents( void )
  279. {
  280.     OSErr    anErr = noErr;
  281.     
  282.     static    long        gHasAppleEvents = kFlagNotSet;
  283.     
  284.     if ( gHasAppleEvents == kFlagNotSet )
  285.     {
  286.         if ( GestaltAvailable() )
  287.         {
  288.             long    response;
  289.             
  290.             if ( Gestalt( gestaltAppleEventsAttr, &response ) == noErr )
  291.             {
  292.                 gHasAppleEvents = ( response & (1L << gestaltAppleEventsPresent) ) != 0;
  293.             }
  294.         }
  295.         else
  296.         {
  297.             gHasAppleEvents = false;
  298.         }
  299.     }
  300.     
  301.     return gHasAppleEvents;
  302. }//end HasAppleEvents
  303.  
  304. //******************************************************************************
  305.  
  306. pascal Boolean FinderCallsAEProcess( void )
  307. {
  308.     OSErr    anErr = noErr;
  309.     
  310.     static    long        gFinderCallsAEProcess = kFlagNotSet;
  311.         
  312.     if ( gFinderCallsAEProcess == kFlagNotSet)
  313.     {
  314.         if ( GestaltAvailable() )
  315.         {
  316.             long    response;
  317.             
  318.             if ( Gestalt( gestaltFinderAttr, &response ) == noErr )
  319.             {
  320.                 gFinderCallsAEProcess = ( response & (1L << gestaltFinderCallsAEProcess) ) != 0;
  321.             }
  322.         }
  323.         else
  324.         {
  325.             gFinderCallsAEProcess = false;
  326.         }
  327.     }
  328.     
  329.     return gFinderCallsAEProcess;
  330. }//end FinderCallsAEProcess
  331.  
  332. //******************************************************************************
  333.  
  334. pascal Boolean FinderIsOSLCompliant( void )
  335. {
  336.     OSErr    anErr = noErr;
  337.     
  338.     static    long        gFinderIsOSLCompliant = kFlagNotSet;
  339.     
  340.     if ( gFinderIsOSLCompliant == kFlagNotSet )
  341.     {
  342.         if ( GestaltAvailable() )
  343.         {
  344.             long    response;
  345.             
  346.             if ( Gestalt( gestaltFinderAttr, &response ) == noErr )
  347.             {
  348.                 gFinderIsOSLCompliant = ( response & (1L << gestaltOSLCompliantFinder) ) != 0;
  349.             }
  350.         }
  351.         else
  352.         {
  353.             gFinderIsOSLCompliant = false;
  354.         }
  355.     }
  356.     
  357.     return gFinderIsOSLCompliant;
  358. }//end FinderIsOSLCompliant
  359.  
  360. //******************************************************************************
  361.  
  362. pascal Boolean FinderUsesIconFamily( void )
  363. {
  364.     OSErr    anErr = noErr;
  365.     
  366.     static    long        gFinderUsesIconFamily = kFlagNotSet;
  367.     
  368.     if ( gFinderUsesIconFamily == kFlagNotSet )
  369.     {
  370.         if ( GestaltAvailable() )
  371.         {
  372.             long    response;
  373.             
  374.             if ( Gestalt( gestaltSystemVersion, &response ) == noErr )
  375.             {
  376.                 gFinderUsesIconFamily = ( response != 0x00000800 );
  377.             }
  378.         }
  379.         else
  380.         {
  381.             gFinderUsesIconFamily = true;
  382.         }
  383.     }
  384.     
  385.     return gFinderUsesIconFamily;
  386. }//end FinderUsesIconFamily
  387.  
  388. //******************************************************************************
  389.  
  390.